package com.hongshan.wallet.core.home

import android.app.Application
import android.databinding.ObservableField
import android.view.View
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.base.MyViewModel
import com.hongshan.wallet.base.SingleLiveEvent
import com.hongshan.wallet.bean.BankCardBean
import com.hongshan.wallet.http.HttpClient
import com.hongshan.wallet.toast
import com.hongshan.wallet.tryRun
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch


/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.home
 * @author   : zhangxx
 * @date     : 2019/3/27
 * @email    : qq957413212@hotmail.com
 * @desc     : //TUDO
 */
class WithdrawalViewModel(application: Application) : MyViewModel(application) {

    val money = ObservableField<String>()
    val allMoney = ObservableField<String>("最多可提现${MyApplication.userInfo.get()?.balance}元")
    val bankName = ObservableField<String>()
    val bankImg = ObservableField<String>()

    //    var grade: List<Grade> = listOf()
    var bankList: List<BankCardBean> = listOf()

    var moneyId = ""
    var bankId = ""

    val showBankCard = SingleLiveEvent<Unit>()
    val showPwdDialog = SingleLiveEvent<Unit>()
    val hidePwdDialog = SingleLiveEvent<Unit>()
    val showErrorDialog = SingleLiveEvent<Unit>()

    val payMethod = View.OnClickListener {
        showBankCard.call()
    }

    var showPwd = View.OnClickListener {
        showPwdDialog.call()
    }

    private fun withdrawal(verify_code: String) {
        GlobalScope.launch(Dispatchers.Main) {
            val net = GlobalScope.async(Dispatchers.IO) {
                tryRun { HttpClient.httpApi.requestWithdrawal(money.get()!!, bankId, verify_code).execute() }
            }
            val response = net.await()
            val result = response?.body()
            if (result?.code == 0) {
                toast("提交成功，请耐心等待审核，约24小时内到账。")
                hidePwdDialog.call()
                finish.call()
            }
        }
    }

    fun getBankCard() {
        GlobalScope.launch(Dispatchers.Main) {
            val net = GlobalScope.async(Dispatchers.IO) {
                tryRun {
                    HttpClient.httpApi.getBindBankCards().execute()
                }
            }
            val response = net.await()
            val result = response?.body()
            if (result?.code==0){
//                grade = result.data.grade
                bankList = result.data.list
            }
        }
    }

    fun checkPwd(it: String) {
        val s = money.get()
        if (s==null||s.isEmpty()||bankId.isEmpty()){
            toast("金额不正确或者未选择提现方式")
            return
        }
        GlobalScope.launch(Dispatchers.Main) {
            val result = GlobalScope.async(Dispatchers.IO) {
                tryRun { HttpClient.httpApi.checkPayPwd(it).execute() }
            }
            val body = result.await()?.body()
            if (body?.code == 0) {
                withdrawal(body.data["verify_code"] ?: error("verify_code == null"))
            } else if (body?.code == -1) {
                showErrorDialog.call()
            } else {
                toast(body?.msg)
            }

        }
    }
}